#include "customlabel.h"
#include <QDir>
#include <QDesktopServices>
#include "global.h"

CustomLabel::CustomLabel(QWidget *parent)
    : QLabel(parent)
{

}

void CustomLabel::setImageMsg(QPixmap pix)
{
    int width = pix.width();
    int height = pix.height();

    // 检查图片的宽度是否大于 400px 或者高度是否大于 300px
    while (width > 400 || height > 300) {
        // 若满足条件，将图片的宽度和高度缩小一倍
        width /= 2;
        height /= 2;
        // 使用 scaled 方法对图片进行缩放，保持纵横比并使用平滑变换
        pix = pix.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    }

    this->setPixmap(pix.scaled(pix.size(),Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

void CustomLabel::setImage(QString iamgeName)
{
    image = iamgeName;
}

void CustomLabel::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(!image.isEmpty()){
        if (event->button() == Qt::LeftButton) {
            QString filePath = QDir::currentPath() + "//" + IMAGE_MSG_PATH + image;
            QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
        }
    }
    QLabel::mouseDoubleClickEvent(event);
}
